from django.shortcuts import render
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.viewsets import ReadOnlyModelViewSet

from areas.models import Area
from .serializers import AreasSerializer, SubAreasSerializer


# Create your views here.

# url(r'^areas/$', views.AreasView.as_view()),
# class AreasView(ListAPIView):
#     """获取省级地区信息"""
#     serializer_class = AreaSerializer
#     queryset = Area.objects.filter(parent=None)
#
#
# # url(r'^areas/(?P<pk>\d+)/$', views.SubAreasView.as_view()),
# class SubAreasView(RetrieveAPIView):
#     """获取市级和区级信息"""
#     serializer_class = SubAreaSerializer
#     queryset = Area.objects.all()

class AreasViewSet(ReadOnlyModelViewSet):
    def get_serializer_class(self):
        if self.action == 'list':
            return AreasSerializer
        else:
            return SubAreasSerializer

    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()
